﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DSPL.Contracts;
using System.IO;

namespace DSPL.Common
{
    /// <summary>
    /// Interaction logic for ImageSaver.xaml
    /// </summary>
    public partial class ImageSaver : UserControl
    {
        public ImageSaver()
        {
            InitializeComponent();
        }

        public string FilePath
        {
            get { return (string)GetValue(FilepathProperty); }
            set { SetValue(FilepathProperty, value); }
        }

        public string FileName
        {
            get { return (string)GetValue(FileNameProperty); }
            set { SetValue(FileNameProperty, value); }
        }

        public ImageSource ImageSourceObject
        {
            get { return (ImageSource)GetValue(ImageSourceObjectProperty); }
            set { SetValue(ImageSourceObjectProperty, value); }
        }

        public Modes FormMode
        {
            get { return (Modes)GetValue(FormModeProperty); }
            set { SetValue(FormModeProperty, value); }
        }


        public bool Enability
        {
            get { return (bool)GetValue(EnabilityProperty); }
            set { SetValue(EnabilityProperty, value); }
        }


        public static readonly DependencyProperty EnabilityProperty = DependencyProperty.Register("Enability", typeof(bool), typeof(ImageSaver), new PropertyMetadata(true));

        public static readonly DependencyProperty FormModeProperty = DependencyProperty.Register("FormMode", typeof(Modes), typeof(ImageSaver), new PropertyMetadata(Modes.Undo, FormModeChangedCallBack));

        public static readonly DependencyProperty ImageSourceObjectProperty = DependencyProperty.Register("ImageSourceObject", typeof(ImageSource), typeof(ImageSaver), new PropertyMetadata(null));

        public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(ImageSaver), new PropertyMetadata("", FileNamePropertyChangedCallback));

        public static readonly DependencyProperty FilepathProperty = DependencyProperty.Register("FilePath", typeof(string), typeof(ImageSaver), new PropertyMetadata("", FilePathPropertyChangedCallback));

        private static void FileNamePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageSaver objImageSaver = d as ImageSaver;

            if (objImageSaver != null)
                if (e.NewValue != null)
                    if (!e.NewValue.Equals(e.OldValue) && !string.IsNullOrEmpty(e.NewValue.ToString()))
                        objImageSaver.SetImage();
        }

        private static void FilePathPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageSaver objImageSaver = d as ImageSaver;

            if (objImageSaver != null)
                objImageSaver.SetImage();
        }

        private static void FormModeChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageSaver objImageSaver = d as ImageSaver;

            if (objImageSaver != null)
                if (!e.NewValue.Equals(e.OldValue))
                    objImageSaver.EnableDissableControls();
        }

        private void EnableDissableControls()
        {
            switch (FormMode)
            {
                case Modes.New:
                case Modes.Edit:
                    Enability = true;
                    break;

                case Modes.None:
                case Modes.Delete:
                case Modes.Approve:
                case Modes.Print:
                case Modes.Save:
                case Modes.Undo:
                case Modes.First:
                case Modes.Previous:
                case Modes.Next:
                case Modes.Last:
                    Enability = false;
                    break;
            }
        }

        private void SetImage()
        {
            if (File.Exists(FilePath))
            {
                ImageSourceObject = BitmapFromUri(new Uri(FilePath));
            }
            else
            {
                ImageSourceObject = null;
            }
        }

        public static ImageSource BitmapFromUri(Uri source)
        {

            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = source;
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            bitmap.EndInit();

            return bitmap;
        }
    }
}
